import 'package:xlsk/api/index.dart';
import 'package:xlsk/config/index.dart';

class classApi{
  factory classApi(){
    final _instance = classApi._();
    return _instance;
  }
  classApi._();

  getClassList(context,data) async{
    var res = await Request(context).post('/Banji/index',data);
    var code = res['code'] is String?num.parse(res['code']):res['code'];
    var result = [];
    if(code == 200){
      result = res['data']['list'];
    }
    return result;
  }

  getClassInfo(context,data) async{
    var today = DateTime.now();
    var daysFromNow = today.add(Duration(days: 7));

    Map<String,dynamic> studentData = data;
    studentData['limit'] = 9999;

    Map<String,dynamic> paikeData = data;
    paikeData['start_date'] = '${today.year}-${today.month}-${today.day}';
    paikeData['jieshu_date'] = '${daysFromNow.year}-${daysFromNow.month}-${daysFromNow.day}';

    var view = await Request(context).post('/Banji/view',data);
    var student = await Request(context).post('/Banji/getStudent',studentData);
    var paike = await Request(context).post('/Banji/getPaike',paikeData);
    var view_code = view['code'] is String?num.parse(view['code']):view['code'];
    var student_code = student['code'] is String?num.parse(student['code']):student['code'];
    var paike_code = paike['code'] is String?num.parse(paike['code']):paike['code'];

    var view_result = {};
    var student_result = [];
    var paike_result = [];
    if(view_code == 200){
      view_result = view['data'];
    }
    if(student_code == 200){
      for(var i in student['data']){
        i['headimg'] = '${Config().baseUrl}${i['headimg']}';
      }
      student_result = student['data'];
    }
    if(paike_code == 200){
      paike_result = paike['data'];
    }
    return {
      "view":view_result,
      "student":student_result,
      "paike":paike_result,
    };
  }

  getTakeName(context,data) async{
    var today = DateTime.now();
    var monthStart = today.add(Duration(days: 1 - today.day));
    var monthEnd = DateTime(today.year,today.month + 1,1).add(Duration(days: -1));

    data['start_date'] = '${monthStart.year}-${monthStart.month}-${monthStart.day}';
    data['jieshu_date'] = '${monthEnd.year}-${monthEnd.month}-${monthEnd.day}';
    var res = await Request(context).post('/Banji/takeName',data);
    var res_code = res['code'] is String?num.parse(res['code']):res['code'];
    var result = [];
    if(res_code == 200){
      result = res['data'];
    }
    return result;
  }

  getTakeName2(context,data) async{
    var res = await Request(context).post('/Banji/takeName',data);
    var res_code = res['code'] is String?num.parse(res['code']):res['code'];
    var result = [];
    if(res_code == 200){
      result = res['data'];
    }
    return result;
  }

  //课程记录-日课表
  getRiKeBiao(context,data) async{
    var res = await Request(context).post('/Banji/takeName',data);
    var res_code = res['code'] is String?num.parse(res['code']):res['code'];
    var result = [];
    if(res_code == 200){
      result = res['data'];
    }
    return result;
  }
}